Note: There are often multiple ways to answer each question.
For the following questions, explain what went wrong and how the program can be fixed.
Load these packages before starting the problems:
library(ggplot2)
library(dplyr)
x
and y
together to get the value of 24:x <- 14
y <- "10"
x + y
## Error in x + y: non-numeric argument to binary operator
1+2
and divide it by 3+4
:((1+2)/(3+4)))
## Error: <text>:1:14: unexpected ')'
## 1: ((1+2)/(3+4)))
## ^
For the rest of the questions, we will use the mtcars
dataset:
data(mtcars)
x
, where L is the number of columns in mtcars
.x <- 1:ncol(mtcars)-1
x
## [1] 0 1 2 3 4 5 6 7 8 9 10
mpg
vs. wt
:ggplot(data = mtcars) +
geom_point(y = mpg, x = wt)
## Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomPoint, : object 'wt' not found
mpg
:ggplot(data = mtcars)
+ geom_histogram(aes(x = mpg))
## Error: Cannot use `+.gg()` with a single argument. Did you accidentally put + on a new line?
mpg
for each value of cyl
, overlay it with points, and add a title to the plot:ggplot(data = mtcars, aes(x = cyl, y = mpg)) +
geom_boxplot() +
geom_point()
labs(title = "Plot of mpg vs. cyl")
## Warning: Continuous x aesthetic -- did you forget aes(group=...)?
## $title
## [1] "Plot of mpg vs. cyl"
##
## attr(,"class")
## [1] "labels"
qsec
vs. wt
, but we want all the points to be colored blue:ggplot(data = mtcars, aes(y = qsec, x = wt)) +
geom_point(aes(col = "blue"))
miles per quart
and display the first 3 rows:mtcars %>% mutate(miles per quart = mpg / 4) %>% head(n = 3)
## Error: <text>:1:25: unexpected symbol
## 1: mtcars %>% mutate(miles per
## ^
mpg
for each value of gear
:mtcars %>% group_by(gear) %>% summarize(mean = mean)
## Error in summarise_impl(.data, dots): Column `mean` is of unsupported type function
hp
and disp
in the dataset:mtcars %>% summarize(max = max(hp, disp))
## max
## 1 472